package com.sanhe.rfmuscle.activity.work;

import static com.menu.app.util.FunctionCode.START1;
import static com.menu.app.util.FunctionCode.START2;

import androidx.annotation.NonNull;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.menu.app.MyApp;
import com.menu.app.activity.BaseCustomInfoActivity;
import com.menu.app.adapter.BaseCusRecordAdapter;
import com.menu.app.bean.DayRecord;
import com.menu.app.db.bean.CusUsage;
import com.menu.app.db.bean.Custom;
import com.menu.app.db.bean.Doc;
import com.menu.app.fragment.BaseDialogFragment;
import com.menu.app.model.Models;
import com.menu.app.util.port.SerialManage;
import com.plata.base.aclr.common.SilentTask;
import com.plata.base.aclr.common.TaskEngine;
import com.sanhe.rfmuscle.activity.BoardActivity;
import com.sanhe.rfmuscle.adapter.CusRecordAdapter;
import com.sanhe.rfmuscle.http.PdfBean;
import com.sanhe.rfmuscle.http.PdfReq;
import com.sanhe.rfmuscle.view.ParamDlg;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.drakeet.support.toast.ToastUtil;

public class CustomInfoActivity extends BaseCustomInfoActivity {
    @Override
    protected BaseCusRecordAdapter getAdapter() {
        return new CusRecordAdapter(this, this, dao, custom, binding.list, isAdmin);
    }

    @Override
    protected String createPdf(Doc doc, Custom custom, List<DayRecord> list) {
        PdfBean bean = new PdfReq(doc.serverId, custom.serverId, list).req();
        if (bean != null && bean.isValid()) {
            return bean.url;
        }
        return null;
    }

    @Override
    protected List<DayRecord> combine(List<List<CusUsage>> l, Map<String, Integer> map) {
        List<DayRecord> list = new LinkedList<>();
        for (String key : map.keySet()) {
            DayRecord r = new DayRecord();
            r.setDay(key);
            r.record = l.get(map.get(key));
            list.add(r);
        }
        Iterator<DayRecord> it = list.iterator();
        while (it.hasNext()) {
            DayRecord r = it.next();
            if (r.img.isEmpty() && (r.record == null || r.record.isEmpty())) {
                it.remove();
            }
        }
        Collections.sort(list, (o1, o2) -> {
            long t1 = date2Ms(o1.getDay(0));
            long t2 = date2Ms(o2.getDay(0));
            if (t1 < t2) {
                return 1;
            } else if (t1 == t2) {
                return 0;
            }
            return -1;
        });
        return list;
    }

    @Override
    public void showReport(DayRecord record) {

    }

    @Override
    public void showParam(List<CusUsage> item) {
        ParamDlg dlg = new ParamDlg();
        dlg.setParam(this, item, custom, isAdmin);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (isOk && value instanceof CusUsage) {
                    toWork((CusUsage) value);
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "param");
    }

    @Override
    protected boolean isStarted() {
        if (!Models.Companion.get().getStartEvent().getValue()) {
            ToastUtil.showShortToast(CustomInfoActivity.this, com.menu.app.R.string.disconnect);
            return false;
        }
        return true;
    }

    @Override
    protected boolean checkHandle(Runnable runnable) {
        return true;
    }

    @Override
    public void sendTouchEvent(byte data_H, byte data_L) {
        byte[] tmp = {START1, START2, 6, -125, 16, 1, 1, data_H, data_L};
        SerialManage.getInstance().send(tmp);
    }

    @Override
    public void startWork(CusUsage usage) {
        showLoading();
        TaskEngine.getInstance().submit(new SilentTask("") {
            @Override
            public void runInBackground() throws Exception {
                String mode = usage.info.get("mode") + "";
                switch (mode) {
                    case "1":
                        sendTouchEvent((byte) 0, (byte) 4);
                        break;
                    case "2":
                        sendTouchEvent((byte) 0, (byte) 2);
                        break;
                    case "3":
                        sendTouchEvent((byte) 0, (byte) 3);
                        break;
                }
                Thread.sleep(100);
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        hideLoading();
                        MyApp.getApp().setIgnore(false);
                        BoardActivity.custom = custom;
                    }
                });
            }
        });
    }

    @Override
    public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {

    }
}
